package net.j4love.spring.boot.shutdownhook;

import org.springframework.beans.factory.DisposableBean;

/**
 * @author He Peng
 * @create 2017-05-19 14:13
 * @update 2017-05-19 14:13
 * @see
 */

//@Component
public class MyDisposableBean implements DisposableBean {

    private Thread thread;
    private volatile boolean destroy = false;

    public MyDisposableBean() {

        this.thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!MyDisposableBean.this.destroy) {
                    System.err.println(MyDisposableBean.this.thread.getName() + "  run");
                }
            }
        }, "MyDisposableBean-Embedded-Thread");

        this.thread.start();
    }

    @Override
    public void destroy() throws Exception {

        this.destroy = true;
        System.out.println("*************************************************** \r\n" +
                    "MyDisposableBean shutdown hook");
    }
}
